package com.sitech.ibnms.util.charts.am.line;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sitech.ibnms.util.RandomGUID;
import com.sitech.ibnms.util.charts.am.AmChartCommon;
import com.sitech.ibnms.util.charts.domain.HIS_DATA_DOMAIN;

public class AmChartLine {
	public AmChartLine() {
	}

	public static void main(String[] args) {
		AmChartLine amChartLine = new AmChartLine();
		AmChartLineParameter parameter = new AmChartLineParameter();
		parameter.setUnit("RMB");

		List<HIS_DATA_DOMAIN> hisDataDomainList = new ArrayList<HIS_DATA_DOMAIN>();
		HIS_DATA_DOMAIN his_data_domain = new HIS_DATA_DOMAIN();
		his_data_domain.setCLL_TIME("2011-11-11 00:00:00");
		his_data_domain.setKPI_VALUE("99");
		HIS_DATA_DOMAIN his_data_domain1 = new HIS_DATA_DOMAIN();
		his_data_domain1.setCLL_TIME("2011-11-12 00:00:00");
		his_data_domain1.setKPI_VALUE("999");
		hisDataDomainList.add(his_data_domain);
		hisDataDomainList.add(his_data_domain1);
		// String xx = amChartLine.getAmChartLineHtml(parameter,
		// hisDataDomainList);
		String xx = amChartLine.getAmLineDate(parameter, hisDataDomainList, "");
		System.out.println(xx);
	}

	public String getAmChartLineHtml(AmChartLineParameter parameter,
			List<HIS_DATA_DOMAIN> hisDataDomainList, String lineCount) {
		String amline_data = getAmLineDate(parameter, hisDataDomainList,
				lineCount);
		String amline_settings = getAmLineSettings(parameter);
		String amLineHtml = AmChartCommon.getScript(parameter).replaceAll(
				"#amline_data#", amline_data);
		amLineHtml = amLineHtml
				.replaceAll("#amline_settings#", amline_settings);
		amLineHtml = amLineHtml.replaceAll("##", "\\\\\"");
		return amLineHtml;
	}

	public String getAmLineDate(AmChartLineParameter parameter,
			List<HIS_DATA_DOMAIN> hisDataDomainList, String lineCount) {

		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("chart");
		Element series = root.addElement("series");
		Element graphs = root.addElement("graphs");
		if (lineCount.equals("1")) {
			String title = "title";
			if (parameter != null) {
				title = parameter.getTitle();
				title = title != null ? title : "title";
			}
			Element graph = graphs.addElement("graph");
			graph.addAttribute("gid", "1");
			graph.addAttribute("title", title);
			if (hisDataDomainList != null && hisDataDomainList.size() > 0) {
				for (int i = 0; i < hisDataDomainList.size(); i++) {
					HIS_DATA_DOMAIN his_data_domain = hisDataDomainList.get(i);
					setAmLineDate(series, graph, his_data_domain);
				}
			} else {
				setAmLineDate(series, graph, null);
			}

		} else {
			String title1 = "title1";
			String title2 = "title2";
			if (parameter != null) {
				title1 = parameter.getTitle1();
				title1 = title1 != null ? title1 : "title1";

				title2 = parameter.getTitle2();
				title2 = title2 != null ? title2 : "title2";

			}
			Element graph1 = graphs.addElement("graph");
			graph1.addAttribute("gid", "1");
			graph1.addAttribute("title", title1);
			Element graph2 = graphs.addElement("graph");
			graph2.addAttribute("gid", "2");
			graph2.addAttribute("title", title2);
			if (hisDataDomainList != null && hisDataDomainList.size() > 0) {
				for (int i = 0; i < hisDataDomainList.size(); i++) {
					HIS_DATA_DOMAIN his_data_domain = hisDataDomainList.get(i);
					setAmLineDate(series, graph1, graph2, his_data_domain);
				}
			} else {
				setAmLineDate(series, graph1, graph2, null);
			}

		}
		String amline_data = document.asXML();
		amline_data = formatStringData(amline_data);
		return amline_data;
	}

	private void setAmLineDate(Element series, Element graph,
			HIS_DATA_DOMAIN his_data_domain) {
		String cll_time = "";
		String kpi_value = "";
		String kpi_value1 = "";
		String kpi_value2 = "";
		if (his_data_domain != null) {
			cll_time = his_data_domain.getCLL_TIME();
			cll_time = cll_time != null ? cll_time.split(" ")[1] : "";

			kpi_value = his_data_domain.getKPI_VALUE();
			kpi_value = kpi_value != null ? kpi_value : "";

			kpi_value1 = his_data_domain.getKPI_VALUE1();
			kpi_value1 = kpi_value1 != null ? kpi_value1 : "";

			kpi_value2 = his_data_domain.getKPI_VALUE2();
			kpi_value2 = kpi_value2 != null ? kpi_value2 : "";
		}

		String xid_id = "xid";
		String xid_value =  RandomGUID.getRandomGUID();
		Element seriesValue = series.addElement("value");
		Element graphValue = graph.addElement("value");
		seriesValue.addAttribute(xid_id, xid_value);
		seriesValue.addText(cll_time);
		graphValue.addAttribute(xid_id, xid_value);
		graphValue.addText(kpi_value);
	}

	private void setAmLineDate(Element series, Element graph1, Element graph2,
			HIS_DATA_DOMAIN his_data_domain) {
		String cll_time = "";
		String kpi_value1 = "";
		String kpi_value2 = "";
		if (his_data_domain != null) {
			cll_time = his_data_domain.getCLL_TIME();
			cll_time = cll_time != null ? cll_time.split(" ")[1] : "";

			kpi_value1 = his_data_domain.getKPI_VALUE1();
			kpi_value1 = kpi_value1 != null ? kpi_value1 : "";

			kpi_value2 = his_data_domain.getKPI_VALUE2();
			kpi_value2 = kpi_value2 != null ? kpi_value2 : "";
		}

		RandomGUID randomGUID = new RandomGUID();
		String xid_id = "xid";
		String xid_value = randomGUID.getRandomGUID();
		Element seriesValue = series.addElement("value");
		seriesValue.addAttribute(xid_id, xid_value);
		seriesValue.addText(cll_time);

		Element graphValue1 = graph1.addElement("value");
		graphValue1.addAttribute(xid_id, xid_value);
		graphValue1.addText(kpi_value1);

		Element graphValue2 = graph2.addElement("value");
		graphValue2.addAttribute(xid_id, xid_value);
		graphValue2.addText(kpi_value2);

	}

	public String getAmLineSettings(AmChartLineParameter parameter) {
		String unit = "";
		String url1 = "";
		String urlName1 = "";
		String url2 = "";
		String urlName2 = "";
		if (parameter != null) {
			unit = parameter.getUnit();
			unit = unit != null ? unit : "";
			url1 = parameter.getUrl1();
			url1 = url1 != null ? url1 : "";
			urlName1 = parameter.getUrlName1();
			urlName1 = urlName1 != null ? urlName1 : "";

			url2 = parameter.getUrl2();
			url2 = url2 != null ? url2 : "";
			urlName2 = parameter.getUrlName2();
			urlName2 = urlName2 != null ? urlName2 : "";
		}
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("settings");

		Element font = root.addElement("font");
		addUserDate(font, "Tahoma");

		Element background = root.addElement("background");
		Element color = background.addElement("color");
		addUserDate(color, "#FFFFFF,#0066CC");
		Element alpha = background.addElement("alpha");
		addUserDate(alpha, "10");
		Element border_alpha = background.addElement("border_alpha");
		addUserDate(border_alpha, "0");

		Element grid = root.addElement("grid");
		Element gx = grid.addElement("x");
		/*
		 * Element approx_count = gx.addElement("approx_count");
		 * addUserDate(approx_count,"1");
		 */
		Element dashed = gx.addElement("dashed");
		addUserDate(dashed, "1");

		Element y_left = grid.addElement("y_left");
		Element approx_count = y_left.addElement("approx_count");
		addUserDate(approx_count, "4");

		Element plot_area = root.addElement("plot_area");
		Element plot_area_color = plot_area.addElement("color");
		plot_area_color.addText("#eaeaea");
		Element plot_area_alpha = plot_area.addElement("alpha");
		plot_area_alpha.addText("50");
		Element plot_area_border_color = plot_area.addElement("border_color");
		plot_area_border_color.addText("#000000");
		Element plot_area_border_alpha = plot_area.addElement("border_alpha");
		plot_area_border_alpha.addText("50");
		Element plot_area_margins = plot_area.addElement("margins");
		Element plot_area_margins_left = plot_area_margins.addElement("left");
		plot_area_margins_left.addText("45");
		Element plot_area_margins_top = plot_area_margins.addElement("top");
		plot_area_margins_top.addText("20");
		Element plot_area_margins_right = plot_area_margins.addElement("right");
		plot_area_margins_right.addText("20");
		Element plot_area_margins_bottom = plot_area_margins
				.addElement("bottom");
		plot_area_margins_bottom.addText("0");

		Element legend = root.addElement("legend");
		Element x = legend.addElement("x");
		Element y = legend.addElement("y");
		Element values = legend.addElement("values");
		Element enabled = values.addElement("enabled");
		addUserDate(enabled, "true");
		Element align = values.addElement("align");
		addUserDate(align, "left");
		Element text = values.addElement("text");
		addUserDate(text, "#CDATA#");

		Element balloon = root.addElement("balloon");
		Element balloon_enabled = balloon.addElement("enabled");
		balloon_enabled.addText("true");
		Element only_one = balloon.addElement("only_one");
		only_one.addText("true");
		Element on_off = balloon.addElement("on_off");
		on_off.addText("true");
		/*
		 * Element corner_radius = balloon.addElement("corner_radius");
		 * corner_radius.addText("3");
		 */
		Element border_width = balloon.addElement("border_width");
		border_width.addText("2");
		Element balloon_color = balloon.addElement("color");
		balloon_color.addText("0ssee0e0");

		Element labels = root.addElement("labels");
		if (url1 != null && !"".equals(url1)) {
			Element label = labels.addElement("label");
			label.addAttribute("lid", "0");
			Element label0X = label.addElement("x");
			label0X.setText("10");
			Element label0Y = label.addElement("y");
			label0Y.setText("0");
			Element label0Width = label.addElement("width");
			Element label0Align = label.addElement("align");
			label0Align.setText("right");
			Element label0Text_color = label.addElement("text_color");
			Element label0text_size = label.addElement("text_size");
			label0text_size.setText("11");
			Element label0text = label.addElement("text");
			label0text.setText("#CDATAURL1#");
		}
		
		if (url2 != null && !"".equals(url2)) {
			Element label = labels.addElement("label");
			label.addAttribute("lid", "1");
			Element label0X = label.addElement("x");
			label0X.setText("10");
			Element label0Y = label.addElement("y");
			label0Y.setText("20");
			Element label0Width = label.addElement("width");
			Element label0Align = label.addElement("align");
			label0Align.setText("right");
			Element label0Text_color = label.addElement("text_color");
			Element label0text_size = label.addElement("text_size");
			label0text_size.setText("11");
			Element label0text = label.addElement("text");
			label0text.setText("#CDATAURL2#");
		}
		
		
		

		Element indicator = root.addElement("indicator");
		Element indicator_enable = indicator.addElement("indicator");
		indicator_enable.addText("false");
		Element zoomable = indicator.addElement("zoomable");
		zoomable.addText("true");

		Element strings = root.addElement("strings");
		Element no_data = strings.addElement("no_data");
		addUserDate(no_data, "没有数据!");
		String amline_settings = document.asXML();
		amline_settings = amline_settings.replaceAll("#CDATA#",
				"<![CDATA[:{value}" + unit + "]]>");
		amline_settings = amline_settings.replaceAll("#CDATAURL1#",
				"<![CDATA[<a href=##" + url1 + "## target=##_blank##><u>"
						+ urlName1 + "</u></a>]]>");
		amline_settings = amline_settings.replaceAll("#CDATAURL2#",
				"<![CDATA[<a href=##" + url2 + "## target=##_blank##><u>"
						+ urlName2 + "</u></a>]]>");
		amline_settings = formatStringData(amline_settings);

		return amline_settings;
	}

	private void addUserDate(Element element, String userDate) {
		element.setText(userDate);
	}

	/*
	 * private void setLabel(Element rootLabel,){ }
	 */

	private String formatStringData(String string) {
		String[] stringList = string.split("\n");
		string = stringList[0] + stringList[1];
		string = string.replace("\"", "##");
		return string;
	}

}
